package br.com.pucrs.aa;

import jason.environment.grid.GridWorldView;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class StreetView extends GridWorldView {

	private static final long serialVersionUID = 1L;
	StreetModel sModel;

	public StreetView(StreetModel model) {
		super(model, "Traffic Control", 700);
		sModel = model;
		defaultFont = new Font("Arial", Font.BOLD, 9); // change default font
		setVisible(true);
		repaint();
	}

	/** draw application objects */
	@Override
	public void draw(Graphics g, int x, int y, int object) {
		switch (object) {
		case StreetModel.OBSTACLE:
			drawObstacle(g, x, y);
			break;
		}
	}

	@Override
	public void drawAgent(Graphics g, int x, int y, Color c, int id) {
		redrawSemaphores(g);
		//sModel.changedSemaphore
		c = Color.red;
		super.drawAgent(g, x, y, c, -1);
		g.setColor(Color.black);
		super.drawString(g, x, y, defaultFont, "Walker" + id);
	}

	public void drawObstacle(Graphics g, int x, int y) {
		g.setColor(Color.green);
		int[] vx = new int[4];
		int[] vy = new int[4];
		vx[0] = x * cellSizeW + (cellSizeW / 2);
		vy[0] = y * cellSizeH;
		vx[1] = (x + 1) * cellSizeW;
		vy[1] = y * cellSizeH + (cellSizeH / 2);
		vx[2] = x * cellSizeW + (cellSizeW / 2);
		vy[2] = (y + 1) * cellSizeH;
		vx[3] = x * cellSizeW;
		vy[3] = y * cellSizeH + (cellSizeH / 2);
		g.fillPolygon(vx, vy, 4);
	}

	public void redrawSemaphores(Graphics g) {
		for (Stop stop : sModel.getSemaphores()) {
			Color signalColor = Color.darkGray;
			if (stop.getSemaphoreColor() == Signal.RED) {
				signalColor = Color.RED;
			} else if (stop.getSemaphoreColor() == Signal.GREEN) {
				signalColor = Color.GREEN;
			}
			g.setColor(signalColor);
			g.drawRect(stop.getLocation().x * cellSizeW + 2, stop.getLocation().y * cellSizeH + 2, cellSizeW - 4, cellSizeH - 4);
			g.setColor(Color.white);
			g.drawRect(stop.getLocation().x * cellSizeW + 3, stop.getLocation().y * cellSizeH + 3, cellSizeW - 6, cellSizeH - 6);
			g.setColor(signalColor);
			g.drawRect(stop.getLocation().x * cellSizeW + 4, stop.getLocation().y * cellSizeH + 4, cellSizeW - 8, cellSizeH - 8);
		}
		sModel.setChangedSemaphore(false);
	}
}
